package com.dxx.cloud.admin.controller.admin;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxx.cloud.admin.module.entity.CustomerEntity;
import com.dxx.cloud.admin.module.request.IdRequest;
import com.dxx.cloud.admin.service.ICustomerService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 37048
 */
@RestController
@RequestMapping("/customer")
public class AdminCustomerController {
    @Resource
    private ICustomerService customerService;

    @PostMapping("/pageList")
    @Operation(summary = "客户列表", description = "客户列表")
    public TableDataInfo<CustomerEntity> pageList(@RequestBody PageQuery request) {
        final Page<CustomerEntity> customerEntityPage = new Page<>(request.getPageNum(), request.getPageSize());
        customerEntityPage.setOrders(List.of(OrderItem.desc("id")));
        final Page<CustomerEntity> page = customerService.page(customerEntityPage);
        return TableDataInfo.build(page);
    }

    @PostMapping("/detailById")
    @Operation(summary = "查询客户", description = "查询客户")
    public R<CustomerEntity> detailById(@RequestBody IdRequest request) {
        return R.ok(customerService.getById(request.getId()));
    }

    @PostMapping("/updateById")
    @Operation(summary = "更新客户", description = "更新客户")
    public R<Boolean> updateById(@RequestBody CustomerEntity request) {
        Assert.notNull(request.getId(),"用户id不能为空");
        return R.ok(customerService.updateById(request));
    }
}
